package com.cf.controller;

import com.cf.entity.Result;
import com.cf.entity.Role;
import com.cf.entity.User;
import com.cf.service.RoleService;
import com.cf.service.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    //    分页查询
    @RequestMapping("/selectUser")
    public Object selectUser(int pagenum,int pagesize,String username){
        Map map=new HashMap();
        map.put("username",username);
        PageInfo<User> page = userService.selectUser(pagenum,pagesize,map);
        if (page.getList().size() != 0){
            return new Result(200,"查询用户信息成功",page);
        }else {
            return new Result(400,"查询用户信息失败",null);
        }
    }
    @RequestMapping("/showimg")
    public void showimg(String filename, HttpServletResponse response, HttpServletRequest request)throws Exception{
        String img = request.getRealPath("img");
        File file = new File(img, filename);
        FileInputStream fis = new FileInputStream(file);
        response.setHeader("Content-Disposition", "filename=" + filename);
        ServletOutputStream os = response.getOutputStream();
        IOUtils.copy(fis, os);
        os.close();
        fis.close();
    }
    //单独上传图片，把上传后的图片名称返回给前端
    @RequestMapping("/upload")
    public Object uploadFile(MultipartFile file, HttpServletRequest request) throws Exception {
        String fname = file.getOriginalFilename();
        if(fname.contains("/")){
            fname = fname.substring(fname.lastIndexOf("/")+1);
        }
        fname =UUID.randomUUID().toString() + fname;
        String img = request.getRealPath("img");
        System.out.println(img.toString());
        file.transferTo(new File(img, fname));
        return new Result(200,fname,null);
    }
    //    个人信息的修改
    @RequestMapping("/updateUser")
    public Object updateUser(User user){
        int i = userService.updateUser(user);
        System.out.println(user.toString());
        if (i!=0){
            return new Result(200,"修改成功",null);
        }else {
            return new Result(400,"修改失败",null);
        }
    }
    //    注销用户
    @RequestMapping("/delUser")
    public Object delUser(long id){
        int i = userService.delUser(id);
        if (i!=0){
            return new Result(200,"删除成功",null);
        }else {
            return new Result(400,"删除失败",null);
        }
    }
    //    根据编号查询
    @RequestMapping("/selectById")
    public Object selectById(long id){
        User user = userService.selectUserById(id);
        if(user!=null){
            return new Result(200,"查询当前用户成功",user);
        }else {
            return new Result(400,"查询当前用户失败",null);
        }
    }
    //    查询用户角色
    @RequestMapping("/selectuserrole")
    public Object selectuserrole(long id){
        User user=userService.selectRoleById(id);
        List<Role> role = roleService.selectRoles();
        Map map=new HashMap();
        map.put("user",user);
        map.put("role",role);
        return new Result(200,"查询成功",map);
    }
    //给用户分配角色
    @RequestMapping("/insertRole")
    public Object insertRole(long userid,long[] roleid){
//        userService.insertRole(userid,roleid,map);
        int role = userService.insertRole(userid, roleid);
        if(role!=0) {
            return new Result(200, "分配成功", null);
        }else {
            return new Result(400, "分配失败", null);
        }
    }
    //    添加管理员
    @RequestMapping("/addManager")
    public Object addManager(User user){
        int i = userService.addManager(user);
        if (i==0){
            return new Result(400,"添加失败",null);
        }else {
            return new Result(200,"添加成功",null);
        }
    }
    //    根据编号查询密码
    @RequestMapping("/selectPassword")
    public Object selectPassword(long id){
        String s = userService.selectPassword(id);
        if(s!=null){
            return new Result(200,"密码查询成功",s);
        }else {
            return new Result(400, "密码查询失败", null);
        }
    }


}
